/*
 * 版权所有 (C) 2013 长沙市红耀通信息技术有限公司。保留所有权利。
 *
 * http://www.zhiqim.com/fadfox/ 欢迎加盟[知凡狐]兴趣小组。
 *
 * 创建说明： 2013-6-29 由 zhichenggang创建
 */
package net.redxyz.repost.resource.action;

import java.util.ArrayList;
import java.util.List;

import net.redxyz.repost.resource.SessionUser;
import net.redxyz.repost.resource.model.ItemDelistInfo;
import net.redxyz.top.TaobaoService;
import net.redxyz.top.data.item.Item;

import org.zhiqim.httpd.HttpRequest;
import org.zhiqim.httpd.context.core.Action;

/**
 * 宝贝分布表
 *
 * @version v7.0.1 @author zouzhigang 2021-7-30 新建与整理
 */
public class ItemDistributeAction implements Action
{
    public void execute(HttpRequest request) throws Exception
    {
        SessionUser sessionUser = request.getSessionUser(SessionUser.class);
        String nick = sessionUser.getNick();
        String sessionId = sessionUser.getSessionId();

        List<Item> onsaleItemList = TaobaoService.getItemService().getOnsaleItemListAll(nick, sessionId);

        List<ItemDelistInfo> list = bulidItemDelistInfo(onsaleItemList);
        request.setAttribute("list", list);
    }

    private List<ItemDelistInfo> bulidItemDelistInfo(List<Item> itemList)
    {
        List<ItemDelistInfo> itemDelistList = new ArrayList<ItemDelistInfo>();
        for (int i = 0; i <= 9; i++)
        {
            ItemDelistInfo itemDelistInfo = new ItemDelistInfo("0" + i);
            itemDelistInfo.setTotalAll(itemList.size());
            itemDelistList.add(itemDelistInfo);
        }

        for (int i = 10; i <= 23; i++)
        {
            ItemDelistInfo itemDelistInfo = new ItemDelistInfo("" + i);
            itemDelistInfo.setTotalAll(itemList.size());
            itemDelistList.add(itemDelistInfo);
        }

        for (Item item : itemList)
        {
            int week = item.getDelistWeek();
            if (week == 0)
                continue;// 这种情况是淘宝返回的delistTime为空的情况，要忽略

            String hour = item.getDelistHour();
            ItemDelistInfo itemKey = new ItemDelistInfo(hour);
            int ind = itemDelistList.indexOf(itemKey);
            ItemDelistInfo idInfo = itemDelistList.get(ind);
            idInfo.addData(week);
        }

        ItemDelistInfo totalIdInfo = new ItemDelistInfo("总计");
        totalIdInfo.setTotalAll(itemList.size());
        for (ItemDelistInfo idInfo : itemDelistList)
        {
            totalIdInfo.setMon(totalIdInfo.getMon() + idInfo.getMon());
            totalIdInfo.setTue(totalIdInfo.getTue() + idInfo.getTue());
            totalIdInfo.setWed(totalIdInfo.getWed() + idInfo.getWed());
            totalIdInfo.setThu(totalIdInfo.getThu() + idInfo.getThu());
            totalIdInfo.setFir(totalIdInfo.getFir() + idInfo.getFir());
            totalIdInfo.setSat(totalIdInfo.getSat() + idInfo.getSat());
            totalIdInfo.setSun(totalIdInfo.getSun() + idInfo.getSun());
        }
        itemDelistList.add(totalIdInfo);

        return itemDelistList;
    }
}
